package com.yingxin.yxerp.module.activiti.entity.builder;

import cn.hutool.core.util.ObjectUtil;
import com.yingxin.yx.framework.commons.activiti.dto.ActivitiInstanceDto;
import com.yingxin.yx.framework.commons.activiti.dto.UserProcessDto;
import com.yingxin.yxerp.module.activiti.entity.ActivitiBizInfo;
import com.yingxin.yxerp.module.organization.entity.User;

import java.util.List;


public class ProcessInstanceDtoBuilder {

    private ActivitiInstanceDto processInstance;

    public ProcessInstanceDtoBuilder() {
        this.processInstance = new ActivitiInstanceDto();
    }

    public ActivitiInstanceDto getProcessInstance() {
        return processInstance;
    }

    public ProcessInstanceDtoBuilder add(ActivitiBizInfo bizInfo) {
        if (bizInfo != null) {
            this.processInstance.setProcessInstanceId(bizInfo.getProcessInstanceId());
            this.processInstance.setApplicationTime(bizInfo.getCreateTime());
            this.processInstance.setBillType(bizInfo.getBizType());
            this.processInstance.setBillId(bizInfo.getBizId());
            this.processInstance.setBillNo(bizInfo.getBizNo());
            this.processInstance.setProcessApprovalStatus(bizInfo.getApprovalStatus());
        }
        return this;
    }

    public ProcessInstanceDtoBuilder add(UserProcessDto proc) {
        if ( ObjectUtil.isNotEmpty( proc ) ) {
            this.processInstance.setProcessInstanceId(proc.getProcessInstanceId());
            this.processInstance.setApplicationTime(proc.getStartTime());
            this.processInstance.setBillType(proc.getBizType());
            this.processInstance.setBillId(proc.getBizId());
            this.processInstance.setBillNo(proc.getBizNo());
            this.processInstance.setProcessApprovalStatus(proc.getProcessApprovalStatus());
            this.processInstance.setRevocable(proc.getRevocable());
            this.processInstance.setCurrentAssigneeInfo( proc.getCurrentAssigneeInfo() );
        }
        return this;
    }

    public ProcessInstanceDtoBuilder addHitTasks(List<ActivitiInstanceDto> hitTasks) {
        this.processInstance.setHitTasks(hitTasks);
        if (hitTasks != null && hitTasks.size() > 0) {
            ActivitiInstanceDto task = hitTasks.get(hitTasks.size() - 1);
            this.processInstance.setTaskId(task.getTaskId());
            this.processInstance.setTaskName(task.getTaskName());
            this.processInstance.setAssigneeId(task.getAssigneeId());
            this.processInstance.setAssigneeName(task.getAssigneeName());
            this.processInstance.setAssigneeProcessingTime(task.getAssigneeProcessingTime());
            this.processInstance.setAssigneeComment(task.getAssigneeComment());
            this.processInstance.setTaskApprovalStatus(task.getTaskApprovalStatus());
            this.processInstance.setDeleteReason(task.getDeleteReason());
        }
        return this;
    }


    public ProcessInstanceDtoBuilder add(User startUser) {
        if (startUser != null) {
            this.processInstance.setApplicantId(startUser.getId());
            this.processInstance.setApplicantName(startUser.getStaffName());
        }
        return this;
    }

}
